package com.zhangsun.springActiveMq.config;

import com.zhangsun.springActiveMq.common.ActiveMqConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;

/**
 * activeMq配置
 */
@Configuration
public class ActiveMqConfig {

    /**
     * 配置监听Queue的监听容器
     *
     * @param connectionFactory 监听容器
     * @return 监听容器
     */
    @Bean(name = ActiveMqConstant.QUEUE_LISTENER)
    public JmsListenerContainerFactory queueListenerContainer(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(false);//监听的是Queue
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }

    /**
     * 配置监听Topic的监听容器
     *
     * @param connectionFactory 监听容器
     * @return 监听容器
     */
    @Bean(name = ActiveMqConstant.TOPIC_LISTENER)
    public JmsListenerContainerFactory topicListenerContainer(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);//监听的是Topic
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }

}
